package com.taiji.dicom4platform.common.core.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.ClassUtils;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

/**
 * @Description
 * @Author fanxl
 * @Date 2021/5/17
 **/
public class FileUtil {
    private static final Logger LOGGER = LoggerFactory.getLogger(FileUtil.class);


    public static String getStaticPath() {
        String path = ClassUtils.getDefaultClassLoader().getResource("").getPath();
        try {
            path = URLDecoder.decode(path, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        LOGGER.info("path1:" + path);
        if (path.contains("jar")) {
            path = path.substring(0, path.lastIndexOf("."));
            path = path.substring(0, path.lastIndexOf("/")) + "/static/";
            path = path.replace("file:/", "/");

            LOGGER.info("path2:" + path);
            FileUtil.createDir(path);
            return path;
        }
        LOGGER.info("path3:" + System.getProperty("os.name"));
        if (System.getProperty("os.name").contains("dows")) {
            path = path.substring(1, path.length());
        }
        path = path.replace("/target/classes/", "") + "/static/";
        LOGGER.info("path4:" + path);
        FileUtil.createDir(path);
        return path;
    }

    /**
     * 创建目录
     *
     * @param prifix 目录前缀
     * @return
     */
    public static boolean createDir(String prifix) {

        String filepath = prifix;

        File dir = new File(filepath);
        if (dir.exists()) {
            return true;
        } else {
            if (!filepath.endsWith(File.separator)) {
                filepath = filepath + File.separator;
            }
            //创建目录
            if (dir.mkdirs()) {
                return true;
            }
        }
        return false;
    }


}
